package com.open.auth.service.impl;


import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.open.auth.entity.OauthClientDetails;
import com.open.auth.mapper.OauthClientDetailsMapper;
import com.open.auth.resp.OauthClientDetailsDTO;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ClientDetailsServiceImpl extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails>  implements ClientDetailsService{

	private PasswordEncoder passwordEncoder = NoOpPasswordEncoder.getInstance();
	
	@Override
	public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
		if (StringUtils.isEmpty(clientId)) {
            log.error("loadClientByClientId clientId为空");
            throw new ClientRegistrationException("clientId为空");
        }
		OauthClientDetailsDTO clients = baseMapper.getClientByClientId(clientId);
        if (clients != null){
            return clients;
        }
        throw new ClientRegistrationException("client客户端不存在!");
	}

	public PasswordEncoder getPasswordEncoder() {
		return passwordEncoder;
	}

	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

}

